﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class SCJH_Edit : BasePage
    {
        protected Guid JHID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
                BindFirst();
        }

        private void BindFirst()
        {
            if (JHID != Guid.Empty)
            {
                using (SCJHBLL sBLL = new SCJHBLL())
                {
                    liTitle.Text = "修改";
                    SCJH entity = sBLL.GetModel(t => t.ID == JHID);
                    this.txtS_Time.Text = entity.JHKSSJ.ToString("yyyy-MM-dd");
                    this.txtE_Time.Text = entity.JHWCSJ.ToString("yyyy-MM-dd");

                }
                //明细表
                using (SCJHMXBBLL xBLL = new SCJHMXBBLL())
                {
                    int RecordCount = 0;
                    List<XSHTMXBModel> mxbs = xBLL.GetSCJHMXB(JHID, "CPBH", null, 1, out RecordCount);
                    repMXList.DataSource = mxbs;
                    repMXList.DataBind();
                    spanMoney.Text = mxbs.Sum(t => t.SL * t.DJ).Value.ToString("F");
                }
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            bool result = false;
            using (SCJHBLL sBLL = new SCJHBLL())
            {
                SCJH entity = new SCJH();
                if (JHID != Guid.Empty)
                    entity = sBLL.GetModel(t => t.ID == JHID);
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.SCJH), out ywItem, out isAdd);
                    entity.JHZT = 0;
                }
                entity.CZRID = this.currentUser.UserID;
                entity.JHKSSJ = Convert.ToDateTime(txtS_Time.Text.Trim());
                entity.JHWCSJ = Convert.ToDateTime(txtE_Time.Text.Trim());

                List<SCJHMXB> delMXB = entity.SCJHMXB.ToList();//旧的待删明细表
                string[] ids = hidSelIds.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var id in ids)
                {
                    SCJHMXB mxb = new SCJHMXB();
                    mxb.ID = Guid.NewGuid();
                    mxb.HTMXBID = new Guid(id);
                    entity.SCJHMXB.Add(mxb);
                }
                if (JHID != Guid.Empty)
                    result = sBLL.Update<SCJHMXB>(entity, delMXB);
                else
                    result = sBLL.Insert(entity);
            }
            if (result)
            {
                btnCreate.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
        }
    }
}